import Vue from 'vue'
import VueRouter from 'vue-router'
import Store from '../store/index'

import {
  Toast
} from 'vant';

Vue.use(Toast);
Vue.use(VueRouter)

const routes = [{
    path: '/home',
    name: 'home',
    component: () => import('../views/Home.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: "/",
    redirect: "/home"
  }
]

const router = new VueRouter({
  routes
})

//记得引入story
router.beforeEach((to, from, next) => {
  if (/(home)$/.test(to.path)) {
    if (Store.state.userinfo.username == undefined) {
      Toast.success('请先登录后访问');
      next("/login")
      return
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router